package com.example.myFirstWebmagic.pipeline;

import java.util.List;
import java.util.Map;

import com.example.myFirstWebmagic.dailyAirReport.dao.DailyAirQualityReportRepository;
import com.example.myFirstWebmagic.dailyAirReport.entity.EnDailyAirQualityReport;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 
 * ClassName: MysqlPipeline 
 * @Description: 爬虫结果保存到数据库
 * @author xiongyao
 * @date 2017年2月28日
 */

public class MysqlPipeline implements Pipeline {
	
	private final DailyAirQualityReportRepository dailyAirQualityReportRepository;
	
	public MysqlPipeline(DailyAirQualityReportRepository dailyAirQualityReportRepository){
		this.dailyAirQualityReportRepository=dailyAirQualityReportRepository;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public void process(ResultItems resultItems, Task task) {

		// TODO Auto-generated method stub
        //遍历所有结果，输出到控制台
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
        	 List<EnDailyAirQualityReport> collection= (List<EnDailyAirQualityReport>) entry.getValue();
             for(EnDailyAirQualityReport report:collection){
            	 dailyAirQualityReportRepository.save(report);
             }
        }
	}

}
